import { _decorator, Button, Component, director, Label, Node } from 'cc';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    @property(PlayerController)
    playerCtl: PlayerController;
    @property(Button)
    startBtn: Button;
    @property(Label)
    distance:Label;

    @property(Node)
    uiFailure:Node;
    @property(Node)
    uiSuccess:Node;
    @property(Node)
    uiComplete:Node;

    start() {
        // 开始游戏
        this.startBtn.node.on(Button.EventType.CLICK, this.onStartGame, this);
        // 监听 自定义事件 (关卡失败)
        director.getScene().on('level_failed', this.onLevelFailed, this)
        // 监听 自定义事件 (关卡成功)
        director.getScene().on('level_successful', this.onLevelSuccessful, this)
    }
    update(deltaTime: number) {
        this.distance.string = this.playerCtl.node.position.z.toFixed(1);
    }

    onStartGame(){
        this.playerCtl.isPlaying = true;
        this.startBtn.node.active = false;
    }

    onLevelFailed(){
        this.uiFailure.active = true;
    }

    onLevelSuccessful(){
        if(director.getScene().name == 'level-003'){
            this.uiComplete.active = true;
        }else{
            this.uiSuccess.active = true;
        }
    }


    // 下一关
    onBtnNext(){
        let currentScene = director.getScene().name;
        if(currentScene == 'level-001'){
            director.loadScene('level-002');
        }
        if(currentScene == 'level-002'){
            director.loadScene('level-003');
        }
    }
    // 重玩
    onBtnReplay(){
        director.loadScene(director.getScene().name);
    }
    // 回到主菜单
    onBtnMainMenu(){
        director.loadScene('main');
    }
}


